package com.moming.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class  SqlSessionFactoryUtils {
    //构造方法私有
    private SqlSessionFactoryUtils() {
    }

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            //1.加载mybatis的核心配置文件,获取SqlSessionFactory
            //定义配置文件的路径
            String resource = "mybatis-config.xml";
            //资源加载返回字节输入流
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //获取工厂
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
}
